package com.server.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.entity.CmsCategory;
import com.server.entity.UpmsOrganization;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import io.swagger.annotations.Api;
import com.server.util.Result;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import com.server.entity.CmsArticle;
import com.server.service.ICmsArticleService;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.server.util.BaseController;

/**
 * <p>
 * 文章表 控制器
 * </p>
 *
 * @author
 * @since 2020-04-03
 */
@RestController
@Controller
@AllArgsConstructor
@Slf4j
@Api(value = "文章表管理", tags = "文章表管理")
@RequestMapping("/admin/article")
public class AdminCmsArticleController extends BaseController {

	private ICmsArticleService cmsArticleService;


	/**
     * 查询列表
     *
     * @author
     * @Date 2020-04-03
     */
	@GetMapping("/getListPage")
	@ApiOperation(value = "查询列表")
    @ResponseBody
    public Result<List<CmsArticle>> listPage(@ApiParam(name = "curPage", value = "当前页（不传入则默认1）", required = false)@RequestParam(required = false,defaultValue = "1") long curPage,
									@ApiParam(name = "pageSize", value = "当前页大小（不传入则默认10）", required = false)@RequestParam(required = false,defaultValue = "10") long pageSize) {
		Page<CmsArticle> page = new Page<>(curPage,pageSize);
        return cmsArticleService.findPage(page);
    }

    /**
     * 根据id查询详情
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/findById")
	@ApiOperation(value = "根据id查询详情")
    @ResponseBody
    public Result<CmsArticle> findById(@ApiParam(name = "id", value = "文章表id", required = true) @RequestParam String id) {
        return Result.returnData(cmsArticleService.findById(id));
    }

	 /**
     * 新增
     *
     * @author
     * @Date 2020-04-03
     */
	@PostMapping("/add")
    @ApiOperation(value = "新增")
    @ResponseBody
    public Result add(@ApiParam(name = "entity", value = "新增对象")@Valid @RequestBody CmsArticle entity) {
        if(cmsArticleService.count(new QueryWrapper<CmsArticle>().lambda().eq(CmsArticle::getTitle,entity.getTitle())
        )>0){
            return Result.error("标题不能重复");
        }
        return cmsArticleService.add(entity);
    }

	/**
     * 修改
     *
     * @author
     * @Date 2020-04-03
     */
	@PostMapping("/update")
    @ApiOperation(value = "修改")
    @ResponseBody
    public Result update(@ApiParam(name = "entity", value = "修改对象")@Valid @RequestBody CmsArticle entity) {
        if(entity.getArticleId()==null){
            return Result.error("id不能为空");
        }
        CmsArticle old =  cmsArticleService.getById(entity.getArticleId());
        if(old==null){
            return Result.error("找不到对应记录");
        }
        BeanUtil.copyProperties(entity,old);
        return  cmsArticleService.update(old);
    }

    /**
     * 批量删除
     *
     * @author
     * @Date 2020-04-03
     */
    @DeleteMapping("/del")
	@ApiOperation(value = "批量删除")
    @ResponseBody
    public Result delete(@ApiParam(name = "ids", value = "批量删除", required = true)@Valid @RequestParam String ids) {
        return cmsArticleService.delete(ids)?Result.ok("操作成功"):Result.error("操作失败");
    }

}
